package com.yihu.wlyy.web.patient.hosptail;

import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.address.Hospital;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.service.app.account.DoctorInfoService;
import com.yihu.wlyy.service.app.hospital.HospitalService;
import com.yihu.wlyy.web.BaseController;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2016.08.19.
 */
@Controller
@RequestMapping(value = "/patient/hosptail")
public class HosptailController extends BaseController {
    @Autowired
   private HospitalService hospitalService;

    @Autowired
    private DoctorInfoService doctorInfoService;
    /**
     * 根据市得到区
     * @return
     */
    @RequestMapping(value = "getTownByCityCode")
    @ResponseBody
    public String getTownByCityCode(String city) {
        try {

            JSONArray array = new JSONArray();
            List<Town> list = hospitalService.getTownByCityCode(city);
            if (list != null) {
                for (Town temp : list) {
                    if (temp == null) {
                        continue;
                    }
                    JSONObject json = new JSONObject();
                    json.put("code", temp.getCode());
                    json.put("name", temp.getName());
                    json.put("photo", temp.getPhoto());
                    array.put(json);
                }
            }
            return write(200, "查询成功", "list", array);
        } catch (Exception e) {
            error(e);
            return error(-1, "查询失败");
        }
    }

    /**
     *
     * @param town
     * @return
     */
    @RequestMapping(value = "getHositalByTownCode")
    @ResponseBody
    public String getHositalByTownCode(String town) {
        try {

            JSONArray array = new JSONArray();
            List<Hospital> list = hospitalService.getHositalByTownCode(town);
            if (list != null) {
                for (Hospital temp : list) {
                    if (temp == null) {
                        continue;
                    }
                    JSONObject json = new JSONObject();
                    json.put("code", temp.getCode());
                    json.put("name", temp.getName());
                    json.put("photo", temp.getPhoto());
                    json.put("address", temp.getAddress());
                    array.put(json);
                }
            }
            return write(200, "查询成功", "list", array);
        } catch (Exception e) {
            error(e);
            return error(-1, "查询失败");
        }
    }

    /**
     * 根据类别获取医院列表
     * @param type
     * @param query 查询条件 医院名称
     * @param id
     * @param pageSize 页数
     * @return
     */
    @RequestMapping(value = "/hospital_list")
    @ResponseBody
    public String getHospitalList(
            @RequestParam(required = true) Integer type,
            @RequestParam(required = false) String query,
            @RequestParam(required = true) long id,
            @RequestParam(required = true) Integer pageSize) {
        try {

            Page<Hospital> hospitalList = hospitalService.getHospitalList(query, type, id, pageSize);

            JSONArray array = new JSONArray();
            if (hospitalList != null) {
                for (Hospital hospital : hospitalList) {
                    if (hospital == null) {
                        continue;
                    }
                    JSONObject json = new JSONObject();
                    json.put("id", hospital.getId());
                    json.put("code", hospital.getCode());
                    json.put("name", hospital.getName());
                    json.put("province", hospital.getProvince());
                    json.put("provinceName", hospital.getProvinceName());
                    json.put("city", hospital.getCity());
                    json.put("cityName", hospital.getCityName());
                    json.put("town", hospital.getTown());
                    json.put("townName", hospital.getTownName());
                    json.put("level", hospital.getLevel());
                    String levelName = "";
                    switch (hospital.getLevel()) {
                        case 1:
                            levelName = "医院";
                            break;
                        case 2:
                            levelName = "社区医院";
                            break;
                    }
                    json.put("levelName", levelName);
                    json.put("address", hospital.getAddress());
                    json.put("intro", hospital.getIntro());
                    json.put("photo", hospital.getPhoto());
                    array.put(json);
                }
            }
            return write(200, "查询成功！", "list", array);
        } catch (Exception ex) {
            error(ex);
            return error(-1, "查询失败！");
        }
    }

    /**
     * 根据医院标识获取医生信息
     * @param hospital 医院标识
     * @param query 查询条件 ：医生名称
     * @param id
     * @param pageSize 页数
     * @return
     */
    @RequestMapping(value = "/doctor_list")
    @ResponseBody
    public String getDoctorByHospital(
            @RequestParam(required = false) String hospital,
            @RequestParam(required = false) String query,
            @RequestParam(required = true) long id,
            @RequestParam(required = true) Integer pageSize) {

        try {
            Page<Doctor> doctorList = doctorInfoService.getDoctorListByHospital(query, hospital, id, pageSize);
            JSONArray array = new JSONArray();
            if (doctorList != null) {
                for (Doctor doctor : doctorList) {
                    JSONObject json = new JSONObject();
                    json.put("id", doctor.getId());
                    json.put("code", doctor.getCode());
                    json.put("name", doctor.getName());
                    json.put("photo", doctor.getPhoto());
                    json.put("sex", doctor.getSex());
                    String sexName = "";
                    switch (doctor.getSex()) {
                        case 1:
                            sexName = "男";
                            break;
                        case 2:
                            sexName = "女";
                            break;
                    }
                    json.put("sexName", sexName);
                    json.put("job", doctor.getJob());
                    json.put("jobName", doctor.getJobName());
                    json.put("hospital", doctor.getHospital());
                    json.put("hospitalName", doctor.getHosptialName());
                    json.put("dept", doctor.getDept());
                    json.put("deptName", doctor.getDeptName());
                    array.put(json);
                }
            }
            return write(200, "查询成功！", "list", array);
        } catch (Exception ex) {
            error(ex);
            return error(-1, "查询失败！");
        }

    }
}
